package com.niuxiaofa.newlaw.Activity;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Bean.RewardBean;
import com.niuxiaofa.newlaw.Bean.SignInBean;
import com.niuxiaofa.newlaw.Bean.SignPageBean;
import com.niuxiaofa.newlaw.Bean.TimeofAssessmentBean;
import com.niuxiaofa.newlaw.Network.WisdomLibraryPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.Tools.UUDateUtils;
import com.niuxiaofa.newlaw.View.CommonPopWindow;
import com.niuxiaofa.newlaw.adapter.RewardAdapter;
import com.niuxiaofa.newlaw.adapter.SignInAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import www.linwg.org.lib.LCardView;

public class AnswerQuestionsSignActivity extends BaseActivity implements CommonPopWindow.ViewClickListener, WisdomLibraryPresenter.interface_getSignPage {


    @BindView(R.id.calendar)
    ImageView calendar;


    @BindView(R.id.answer_bottom)
    ImageView answer_bottom;

    @BindView(R.id.signNum)
    TextView signNum;

    SignInAdapter signInAdapter;

    List<SignInBean> signInBeanList = new ArrayList<>();


    @BindView(R.id.sign_recyclerview)
    RecyclerView sign_recyclerview;

    @BindView(R.id.lcard)
    LCardView lcard;

    @BindView(R.id.reward_recyclerview)
    RecyclerView reward_recyclerview;

    List<RewardBean> newsList=new ArrayList<>();

    RewardAdapter rewardAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_answer;
    }

    @Override
    protected void initData() {
        setTitle("答题学法");

        rewardAdapter=new RewardAdapter(newsList, new RewardAdapter.Onclick() {
            @Override
            public void onclick(int c) {

            }
        });


        for (int i = 0; i < 5; i++) {
            RewardBean rewardBean=new RewardBean();
            rewardBean.setAnswer(1);
            rewardBean.setUnlock(false);
            newsList.add(rewardBean);
        }


        RecyclerViewHelp.setRecyclerHorizontal(reward_recyclerview,2).setAdapter(rewardAdapter);




        lcard.setOnClickListener(e->{
            ToActivityUtil.toNextActivity(AnswerQuestionsActivity.class);
        });

        Map map = new HashMap();
        map.put("date", UUDateUtils.getTodayTime2());


        signInAdapter = new SignInAdapter(signInBeanList, new SignInAdapter.Onclick() {
            @Override
            public void onclick(int c) {

            }
        });

        RecyclerViewHelp.setRecyclerHorizontal(sign_recyclerview, 0).setAdapter(signInAdapter);


        //得到签到日期
        WisdomLibraryPresenter.getSignPage(map, this);

        WisdomLibraryPresenter.getSignNum(new WisdomLibraryPresenter.interface_getSignNum() {
            @Override
            public void getResponse(BaseBean u) {
                if (u.getCode() == 1) {
                    signNum.setText(u.getData().toString());
                }
            }
        });

        calendar.setOnClickListener(e -> {
            CommonPopWindow.newBuilder()
                    .setView(R.layout.pop_calendar)
                    .setAnimationStyle(R.style.anim_menu_bottombar)
                    .setBackgroundDrawable(new BitmapDrawable())
                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(getContext(), 200))
                    .setViewOnClickListener(this)
                    .setBackgroundDarkEnable(true)
                    .setBackgroundAlpha(0.7f)
                    .setBackgroundDrawable(new ColorDrawable(999999))
                    .build(this)
                    .showAsBottom(answer_bottom);

        });

    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        calendar.addScheme(new Calendar.Scheme());
        calendar.addScheme(0xFF008800, "假");
        calendar.addScheme(0xFF008800, "节");
        return calendar;
    }

    @Override
    public void getChildView(PopupWindow mPopupWindow, View view, int mLayoutResId) {
        CalendarView calendarView = view.findViewById(R.id.calendarView);


    }

    /**
     * 签到详情
     *
     * @param
     */
    @Override
    public void getResponse(SignPageBean signPageBean) {
        if (signPageBean.getCode() == 1) {

            WisdomLibraryPresenter.getTimeofAssessment(new WisdomLibraryPresenter.interface_getTimeofAssessment() {
                @Override
                public void getResponse(TimeofAssessmentBean timeofAssessmentBean) {
                    if (timeofAssessmentBean.getCode() == 1) {
                        String Examination = UUDateUtils.interceptTime2(timeofAssessmentBean.getData().getAssessmentDay());

                        for (int j = 0; j < 36; j++) {
                            String day = UUDateUtils.tomorrowDateStr(j);

                            SignInBean signInBeans = new SignInBean();
                            signInBeans.setDate(day);
                            signInBeans.setType(0);

                            if (j == 0) {
                                signInBeans.setDate("今天");
                            }
                            if (j == 1) {
                                signInBeans.setDate("明天");
                            }

                            for (int i = 0; i < signPageBean.getData().size(); i++) {

                                String daySign = UUDateUtils.interceptTime2(signPageBean.getData().get(i).getSignTime());

                                if (day.equals(daySign)) {
                                    signInBeans.setType(1);
                                }
                            }

                            //考试日期
                            if (day.equals(Examination)) {
                                signInBeans.setType(2);
                            }

                            signInBeanList.add(signInBeans);
                        }

                        signInAdapter.notifyDataSetChanged();
                    }
                }
            });


        }
    }
}